package Stream流学习;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class groupingBy {


    @Data
    @AllArgsConstructor
    @ToString
    static class Person{
        private String name;
        private String pass;
        private List<String> loveGirls;
    }
    static List<Person> persons;
    static{
        persons = new ArrayList<>();
        persons.add(new Person("SunAo","123", Arrays.asList("小鞠","小李")));
        persons.add(new Person("LiSi","456",Arrays.asList("小青")));
        persons.add(new Person("ZhangSan",null,Arrays.asList("小芳")));
        persons.add(new Person("ZhaoLiu","123", Collections.emptyList()));
    }

    @Test
    public void Collectors类groupingBy方法试用() {
        Map<String, Set<Person>> map = persons.stream()
                .collect(Collectors.groupingBy(Person::getName,
                        ()->{
                            Map<String, Set<Person>> respSet = new HashMap<>();
                            System.out.println("hashcode:"+respSet.hashCode());
                            return respSet;
                        },
                        Collectors.toSet()));
        System.out.println(JSONObject.toJSONString(map));
    }
    @Test
    public void Collectors类mapping方法试用() {
        Map<String, Person> map = persons.stream()
                .collect(Collectors.toMap(Person::getName,v->v,(v1,v2)->v1));
        System.out.println(JSONObject.toJSONString(map));
    }

}
